<template>
	<view class="tab-content">
		<view class=" uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" style-type="button"
				active-color="#18bc9c" @clickItem="onClickItem" />
		</view>
		
		<!-- 滑块视图 -->
		<view v-show="current === 0">
			<scanCode></scanCode>
			<scanInvShelves :getkcFlag="true" :order_type="order_type" :inventory="inventory"  :shelves="shelves" ref="scanIS"></scanInvShelves>
			<view class="swiper-button">
				<!-- <button @click="toOk()">完成</button> -->
				<button @click="toBack()">返回</button>
				<button @click="$refs.scanIS.toAdd()" >添加</button>
			</view>
		</view>
			
		<view v-show="current === 1">
			<scroll-view scroll-y="true">
				<view class="form_list">
					<showMaterialList :dataArr="porderItem" :orderType="order_type" flag='check' ref="showMaterialList" :showShelves="false"></showMaterialList>
				</view>
			</scroll-view>
		</view>	
	</view>
</template>

<script>
	import scanCode from "@/component/scan-code.vue"//因为这个页面没有表头，所以得单独引用
	import scanInvShelves from '@/component/scan-inv-shelves.vue'
	import showMaterialList from "@/component/show-material-list.vue"
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniSegmentedControl,scanInvShelves,showMaterialList,scanCode
		},
		data() {
			return {
				current: 0, /* 选中标签栏的序列,默认显示第一个 */
				tabBars:['扫描','盘点明细'],
				
				porderItem:[],//盘点明细
				
				checkinventory:{},//盘点信息
				inventory:{},//物料明细
				shelves:{},
				
				fqty:'',//库存
				real_quantity:0,//实际库存
				user:uni.getStorageSync('user'),
				order_type:13 //盘点
			}
		},
		computed: {},
		watch:{},
		onLoad(options) { //调用方法并赋值
			this.$Func.setBarTitle('添加盘点明细')
			/* 监听传过来的盘点信息 */
			uni.$once("getCheckItem", (value) => {
				this.checkinventory = value;
				//this.getScanItem()
			})
			/* 监听传过来的盘点明细 */
			uni.$off("checkItems")
			uni.$on("checkItems", (arrs,value) => {
				this.porderItem = arrs;
				this.toOk(value)//点添加按钮，就自动加入数据库
			})
		},
		onShow() {},
		destroyed() {
			uni.$off("checkItems")
		},
		methods: {
			//切换标签内容
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			toBack(){
				uni.navigateBack({
				delta: 1,
				success: function() {
				  //console.log('navigateBack success');
				}
			  });
			},
			async getScanItem(){
				//暂不这样处理，因为有可能加载太多，卡s
				uni.showLoading({
					mask:true
				})
				await this.$Db.selectTableData('check_kc_item','FBillNo',this.checkinventory.FBillNo,'scan_flag',1).then(res=>{
					//取出历史记录，然后重新赋值
					var newArr = res.map(item => {
					  item.unit = item.inv_unit
					  item.number = item.inv_number
					  item.unit_code = item.inv_unit_code
					  item.kc_quantity = item.kc_qty
					  item.real_quantity = item.check_qty
					  item.kc_store_quantity = item.kc_auxqty
					  item.real_store_quantity = item.check_auxqty
					  return item;
					});
					uni.hideLoading()
					this.porderItem = newArr
					this.$refs.scanIS.scanItems = newArr
				})
			},
			//添加物料
			async toOk(pitem) {
				if(!this.porderItem.length){
					this.$Api.msg('明细不能为空','error');
					return ;
				}
				 
				if(JSON.stringify(this.checkinventory) == '{}'){
					this.$Api.msg('请选择盘点记录','error');
					return ;
				}
				
				this.$Db.dealCheckItem(this.checkinventory.FBillNo,pitem)//this.porderItem
				.then(res=>{
					this.$Api.msg("添加成功",'success');
					//this.porderItem = []
				})
				.catch(err=>{
					console.log(err)
					this.$Api.msg('添加失败','error');
					this.$Db.addlog('新增盘点明细',err)
				})
			}
		}
	}
</script>
<style lang="scss">
</style>